package cn.longge.controller;

import cn.longge.entity.Result;
import cn.longge.entity.Statute;
import cn.longge.service.IStatuteService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@RestController
@RequestMapping("manager/statute")
public class StatuteController {

    @Autowired
    private IStatuteService statuteService;

    @RequestMapping("index")
    public ModelAndView index(){
        return new ModelAndView("statute/index");
    }

    @RequestMapping("select/{page}/{pageSize}")
    public Result selectByPage(@PathVariable int page,@PathVariable int pageSize,Integer type){
        PageInfo pageInfo = statuteService.selectByPage(page, pageSize, type);
        return new Result(true,"success",pageInfo);
    }

    @RequestMapping("add")
    public Result add(@RequestBody Statute statute){
        statute.setCreateDate(new Date());
        statute.setUpdateDate(new Date());
        statute.setDelFlag("0");
        statuteService.insertSelective(statute);
        return new Result(true,"success",null);
    }

    @RequestMapping("delete/{id}")
    public Result delete(@PathVariable Long id){
        Statute statute = new Statute();
        statute.setDelFlag("1");
        statute.setId(id);
        statuteService.updateByPrimaryKeySelective(statute);
        return new Result(true,"删除成功",null);
    }

    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(){
        return new ModelAndView("statute/update");
    }

    @RequestMapping("doUpdate")
    public Result doUpdate(@RequestBody Statute statute){
        statute.setUpdateDate(new Date());
        statuteService.updateByPrimaryKeySelective(statute);
        return new Result(true,"success",null);
    }
}
